import axios from "axios";
import { get_token } from "./auth";
import { Message } from 'element-ui'

const instance = axios.create({
    baseURL: 'http://localhost:11093/',
    timeout: 20000,
    //headers: { 'Authorization': get_token() }
});

// 添加请求拦截器
instance.interceptors.request.use(function (config) {
    config.headers.Authorization = get_token();
    //  console.log(config);
    // 在发送请求之前做些什么
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});


instance.interceptors.response.use(function (response) {
    return response.data;
}, function (error) {
    console.log("统一处理异常", error);
    if (error.code === "ERR_NETWORK") {
        Message.error("网络异常");
        return;
    }
    if (error.status === 401) {
        location.href = '/login';
        return;
    }

    return;
});


export default instance